package com.example.webbrowser.Fragment;

import android.app.Activity;

import androidx.fragment.app.Fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;

import androidx.annotation.Nullable;

import com.example.webbrowser.Adapter.HistoryAdapter;
import com.example.webbrowser.Adapter.MarkAdapter;
import com.example.webbrowser.Database.WebHistoryDao;
import com.example.webbrowser.Entity.WebHistory;
import com.example.webbrowser.R;

/**
 * author:陈润嘉
 * data：2021/7/18
 * description：历史界面的组件编写
 */
public class HistoryFragment extends Fragment {

    private WebHistoryDao webHistoryDao;
    private Activity activity;
    private HistoryAdapter historyAdapter;
    private ListView viewlist;
    private boolean isFirstLoading;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.history, container, false);
        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        activity = getActivity();
        webHistoryDao = new WebHistoryDao(getActivity());
        viewlist = getActivity().findViewById(R.id.ListView_History);
        historyAdapter = new HistoryAdapter(getActivity(), R.layout.history_item, webHistoryDao.getWebHistoryList());
        viewlist.setAdapter(historyAdapter);


        //清空按钮操作
        View deleteButton = getActivity().findViewById(R.id.HistoryDeleteAll);
        deleteButton.setOnClickListener(v -> {
            System.out.println("你点击了清空 按钮");
            webHistoryDao.deleteAll();
            viewlist.setAdapter(new HistoryAdapter(getActivity(), R.layout.history_item, webHistoryDao.getWebHistoryList()));
        });


    }

    //用于重新进入页面时刷新数据
    @Override
    public void onResume() {
        super.onResume();
        viewlist = (ListView) getActivity().findViewById(R.id.ListView_History);
        viewlist.setAdapter(new HistoryAdapter(getActivity(), R.layout.history_item, webHistoryDao.getWebHistoryList()));
//
//        if (!isFirstLoading) {
//            //如果不是第一次加载，刷新数据
//
//        }
//        isFirstLoading = false;
    }


    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        viewlist = (ListView) getActivity().findViewById(R.id.ListView_History);
        viewlist.setAdapter(new HistoryAdapter(getActivity(), R.layout.history_item, webHistoryDao.getWebHistoryList()));

    }
}